<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="com.crm.util.RequestEx"%>
<%@ page import="com.crm.service.OfferService"%>
<%@ page import="com.crm.po.Config"%>
<%@ page import="com.crm.util.SESSION"%>
<%@ page import="com.crm.po.Users"%>
<%
	Object obj = request.getSession(true).getAttribute(SESSION.USER_SESSION_KEY);
	Users users = null;
	if(obj != null){
		users = (Users) obj;
	}
	String path = request.getContextPath();
	String treeid = RequestEx.getStringTrim(request.getParameter("treeid"));
	
	OfferService service = new OfferService();
	
	String taxrate = "";
	
	int offerCount = service.getOfferCountByTreeid(treeid, "Y");

%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>  
    <title>CRM</title>
    <link rel="icon" mce_href="<%=path%>/images/title_logo.ico" href="<%=path%>/images/title_logo.ico" type="image/x-ico"/>
    <link rel="shortcut icon" mce_href="<%=path%>/images/title_logo.ico" href="<%=path%>/images/title_logo.ico" type="image/x-ico"/>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>	
	<link rel="stylesheet" type="text/css" href="<%=path%>/js/easyui1.3.2/themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="<%=path%>/js/easyui1.3.2/themes/icon.css">
	<script type="text/javascript" src="<%=path%>/js/easyui1.3.2/jquery-1.8.0.min.js"></script>
	<script type="text/javascript" src="<%=path%>/js/easyui1.3.2/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="<%=path%>/js/easyui1.3.2/locale/easyui-lang-zh_CN.js"></script>
	<script type="text/javascript" src="<%=path%>/js/easyui1.3.2/ajaxfileupload.js"></script>
	<script type="text/javascript" src="<%=path%>/js/My97DatePicker/WdatePicker.js"></script>
	<script type="text/javascript" src="<%=path%>/js/math.js"></script>
	<style type="text/css">
		body,td,th,div{
 			font-size:12px;
		}
	</style>
  </head>
  
  <body style="margin:0;padding:0" class="easyui-layout">
    <script type="text/javascript">

	var datagrid;
	var url = "<%=path%>/offer.do";

	//准备新增数据
	function readySave(){
		document.getElementById("saveAndGoon").style.display="inline";
		$('#recSaveOrUpdate').dialog({  
			title: '新增',  
			width: 430,
			height: 200,
			closed: false,  
			cache: false,   
			modal: true,
			onClose:function(){$('#recSaveOrUpdateForm').form('clear');}
		});
	}
	//新增数据
	function doSave(isContinue){
		var tid = document.getElementById('tid').value;
		var path = document.getElementById('path').value;
		if(tid == null || tid.length <= 0){
			if(path == null || path.length <= 0){
				$.messager.alert('提示', '<br>请选择一个需要上传的文件。', 'info');
				return;
			}
		}
		$.ajaxFileUpload({  
	        url : url + '?method=upload',  
	        secureuri : false,  
	        fileElementId : 'path',  
	        dataType : 'text', //or json xml whatever you like~  
	        success : function(data, status) {
	    		if(data.indexOf('error') < 0){
					if(data.length > 0){
						$('#recSaveOrUpdateForm').form('load',{
		    				fileName: data
		    			});
					}					
	    			$("#recSaveOrUpdateForm").form('submit', { 
	    		        url: url + '?method=saveOrUpdateOffer&treeid=<%=treeid%>',	        
	    				success:function(data){
	    					if('ok' == data){
	    						if(isContinue){
	    							//$('#recSaveOrUpdateForm').form('clear');
	    						}else{
	    							$('#recSaveOrUpdate').dialog('close');
	    						}					
	    						datagrid.datagrid('reload');
	    					}else{
	    						$.messager.alert('提示', data, 'info');
	    					}
	    				},
	    				error : function() {
	    				
	    				}
	    			});
				}else{								
					$.messager.alert('提示', '上传文件失败，请检查您的系统是否出现错误。', 'info');
				}	
	        },  
	        error : function(data, status, e) {  
	        	$.messager.alert('提示', '文件上传出现错误。', 'info');
	        }  
	    });  
		
	}

	//修改数据
	function doUpdate(){
		var node = datagrid.datagrid('getSelected');
		document.getElementById("saveAndGoon").style.display="none";
		$('#recSaveOrUpdate').dialog({  
			title: '修改',  
			width: 430,
			height: 200,
			closed: false,  
			cache: false,   
			modal: true,
			onClose:function(){$('#recSaveOrUpdateForm').form('clear');}
		});
		$('#recSaveOrUpdateForm').form('load',{
			tid: node.tid,
			fileName: node.path,
			name: node.name,		
			remark: node.remark,
			path: node.path
		});	
	}

	//删除数据
	function doDelete(){
		var node = datagrid.datagrid('getSelected');
		//删除确认
		$.messager.confirm('提示','<br>您确定删除选中的数据吗?',function(r){ 
			if(r){ 
				//执行删除
				var tids = "'" + node.tid + "'";

				$.ajax({
					url : url + '?method=deleteOffer&treeid=<%=treeid%>',
					type : 'POST',
					data : {tids : tids},
					success : function(data, textStatus) {
						if('ok' == data){
							datagrid.datagrid('load');
						}else{								
							$.messager.alert('提示', data, 'info');
						}		
					}
				});
			} 
		});
	}
	//查询
	function doQuery(){
		datagrid.datagrid('options').url = url+'?method=getOfferList&treeid=<%=treeid%>&islive=Y';
		var queryParams = datagrid.datagrid('options').queryParams;
		
		if($.trim($("#name").val()).length == 0 && 
		   $.trim($('#remark').val()).length == 0){
			   
		   	queryParams.name = "";
		   	queryParams.remark = "";
		}else{
			queryParams.name = $("#name").val();
			queryParams.remark = $('#remark').val();
		}
		datagrid.datagrid('load');
	}
	
	//定义datagrid列模型
	var columns = [[     	
		{field:'oper',title:'操作',width:90,formatter:formatOper},
		{field:'tid',title:'主键',width:20,hidden:true},
		{field:'path',title:'文件名称',width:200,formatter:formatFile},
		{field:'name',title:'报价单名称',width:200},	
		{field:'remark',title:'备注',width:150},
		{field:'creattime',title:'创建时间',width:115,formatter:formatDate},
		{field:'updatetime',title:'最后修改时间',width:115,formatter:formatDate},
		{field:'creater',title:'创建用户',width:100},
		{field:'updater',title:'最后修改用户',width:100}
	]];

	//入口
	$(function(){
		datagrid = $('#datagrid');
		$(datagrid).datagrid({
			url:url+'?method=getOfferList&treeid=<%=treeid%>&islive=Y', 
			toolbar:'#tb',
			columns:columns,
			title:'', 
			loadMsg:'正在处理...',
			iconCls:'', 
			nowrap: false, 
			selectOnCheck: false,
			checkOnSelect: false,
			rownumbers: true,
			remoteSort: false,
			autoRowHeight: false,
			autoRowWidth: true,             
			animate:true, 
			singleSelect: true,
			collapsible: true, 
			fitColumns: true,//自动充满列
			fit:true,//自动充满浏览器区域
			idField:'tid', 
			showFooter: false,
			striped:true,//斑马线
			pagination:true,
			pageSize:20,
			onClickRow:function(rowIndex, rowData){

			},
			onDblClickRow:function(rowIndex, rowData){
				<%
				if(users.getOfferAuth().indexOf("AUTH_U") >= 0){
				%>
				doUpdate();
				<%	
				}
				%>				
			},
			onLoadSuccess:function(data){
				var p = datagrid.datagrid('getPager');  
				$(p).pagination({					
					pageList:[20, 50, 100, <%=offerCount <= 100 ? "200" : offerCount + ""%>]
			    });  
			}
		}); 

		
	}); 
	
	//格式化创建时间
	function formatDate(value, row){
		value = value + '';
		if(value == null || value.length == 0){
			return "";
		}
		var yy = '';
		var mo = '';
		var da = '';
		
		var ho = '';
		var mm = '';
		var ss = '';

		var dateTime = '';
		if(value.length > 4){
			yy = value.substr(0, 4);
			dateTime = dateTime + yy;
		}
		if(value.length > 6){
			mo = value.substr(4, 2);
			dateTime = dateTime + '-' + mo;
		}
		if(value.length > 8){
			da = value.substr(6, 2);
			dateTime = dateTime + '-' + da;
		}
		
		if(value.length > 10){
			ho = value.substr(8, 2);
			dateTime = dateTime + ' ' + ho;
		}
		if(value.length > 12){
			mm = value.substr(10, 2);
			dateTime = dateTime + ':' + mm;
		}
		if(value.length >= 14){
			ss = value.substr(12, 2);
			dateTime = dateTime + ':' + ss;
		}
		return dateTime;
	}

	//格式化操作
	function formatOper(value, row){
		var updateOper = "<font color='#C0C0C0'>修改</font>";
		var deleteOper = "<font color='#C0C0C0'>删除</font>";
		var downloadOper = "<font color='#C0C0C0'>下载</font>";
		<%
		if(users.getOfferAuth().indexOf("AUTH_U") >= 0){
		%>
			updateOper = "<a href='javascript:doUpdate();'>修改</a>";
		<%
		}
		if(users.getOfferAuth().indexOf("AUTH_D") >= 0){
		%>
			deleteOper = "<a href='javascript:doDelete();'>删除</a>";
		<%
		}
		if(users.getOfferAuth().indexOf("AUTH_X") >= 0){
		%>
		downloadOper = "<a href=\"javascript:doDownload('" + row.path + "');\">下载</a>";
		<%
		}
		%>
		return updateOper + '&nbsp;&nbsp;&nbsp;' + deleteOper + '&nbsp;&nbsp;&nbsp;' + downloadOper;
	}

	//格式化文件图标
	function formatFile(value, row){
		var tempValue = value + '';
		var index = tempValue.lastIndexOf('.');

		var fileImage = '';
		
		if(index >= 0){
			var extName = tempValue.substring(index + 1, tempValue.length);
			if('DOC' == extName.toUpperCase()){
				fileImage = '<%=path%>/images/offer/doc.jpg';
			}else if('EXE' == extName.toUpperCase()){
				fileImage = '<%=path%>/images/offer/exe.jpg';
			}else if('JPG' == extName.toUpperCase()){
				fileImage = '<%=path%>/images/offer/jpg.jpg';
			}else if('PDF' == extName.toUpperCase()){
				fileImage = '<%=path%>/images/offer/pdf.jpg';
			}else if('PPT' == extName.toUpperCase()){
				fileImage = '<%=path%>/images/offer/ppt.jpg';
			}else if('RAR' == extName.toUpperCase() || 'ZIP' == extName.toUpperCase()){
				fileImage = '<%=path%>/images/offer/rar.jpg';
			}else if('TXT' == extName.toUpperCase()){
				fileImage = '<%=path%>/images/offer/txt.jpg';
			}else if('XLS' == extName.toUpperCase() || 'XLSX' == extName.toUpperCase()){
				fileImage = '<%=path%>/images/offer/xls.jpg';
			}else{
				fileImage = '<%=path%>/images/offer/unknown.jpg';
			}
		}else{
			fileImage = '<%=path%>/images/offer/unknown.jpg';
		}
		return "<table><tr height='40px'><td><image src='" 
				+ fileImage + "' border='0' style='width:60px;height:60px;'/></td><td>" 
				+ value + "</td></tr></table>";
	}

	function doDownload(fileName){
		document.getElementById('downloadPath').value = fileName;
		$("#downloadForm").form('submit',{ 
	        url: url + '?method=doDownload',	        
			success:function(data){
				if(!('ok' == data)){					
					$.messager.alert('提示', data, 'info');					
				}
			},
			error : function() {
				$.messager.alert('提示', '下载出现错误,原因可能为:<br>1: 网络连接有异常,<br>2: 下载的文件不存在或已被删除.', 'info');
			}
		});
	}
	</script>

	<div data-options="region:'center',title:'报价单管理'">
	<div style="display:none">
		<form id="downloadForm" name="downloadForm" action="" method="post"  target="_blank">
			<input type="hidden" id="downloadPath" name="downloadPath" value="">
		</form>
	</div>
	<!-- 表格面板 -->
	<table id="datagrid"></table>
	
	<!-- 新增或编辑面板 -->
	<div id="recSaveOrUpdate" class="easyui-dialog" closed="true" data-options="buttons:'#buttonsSaveOrUpdate'" style="padding-top:20px;padding-left:10px;overflow:hidden">
	    <form id="recSaveOrUpdateForm" name="recSaveOrUpdateForm" action="" method="post">
	    	<input type="hidden" id="tid" name="tid" value="">
	    	<input type="hidden" id="fileName" name="fileName" value="">
	    	<table border="0" bordercolor="red">
	    		<tr>  
                    <td>请选择文件</td>
                    <td><input type="file" style="width:320px;height:21px" id="path" name="path"></td>    
                </tr>
	    		<tr height="30px">
	    			<td width="60px" align="right">报价单名称</td>
	    			<td width="60px" colspan="5"><input style="width:320px;height:21px" class="easyui-validatebox" type="text" id="name" name="name" data-options="validType:['length[0,100]']"/></td>
	    		</tr>
	    		<tr height="30px">
	    			<td align="right">备注</td>
	    			<td colspan="5"><input style="width:320px;height:21px" class="easyui-validatebox" type="text" id="remark" name="remark" data-options="validType:['length[0,2000]']"/></td>
	    		</tr>
	    	</table>
	    </form>
	</div>
	<div id="buttonsSaveOrUpdate" style="display:none">
		<a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:return doSave(false)">保存</a>
		<span id="saveAndGoon">
		<a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:return doSave(true)">保存并继续新增</a>
		</span>
		<a href="javascript:void(0)" class="easyui-linkbutton" onclick="javascript:$('#recSaveOrUpdate').dialog('close');">关闭</a>
	</div>

	
	<!-- 增，删，改，查面板 -->
	<div id="tb" style="padding:0px;height:auto">
		<table width="100%" border="0" bordercolor="red">
			<tr>
				<td style="padding-right:0px">
					<%if(users.getOfferAuth().indexOf("AUTH_A") >= 0){
					%>
					<a href="javascript:readySave();" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">上传</a>
					<%
					}else{
					%>
					<a href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'" style="color:#C0C0C0">上传</a>
					<%	
					}%>
				</td>
				<td width="60px" style="padding-left:0px">报价单名称</td>
				<td width="90px"><input id="name" name="name" class="easyui-validatebox" style="width:100px;height:20px"/></td>
				<td width="30px" style="padding-left:0px">备注</td>
				<td width="90px">
					<input id="remark" name="remark" class="easyui-validatebox" style="width:100px;height:20px"/>
				</td>
				<td width="80px" style="padding-left:0px">
					<a href="javascript:doQuery();" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-search'">查询</a>
				</td>								
			</tr>
		</table>
	</div>
	
	
	</div>
  </body>
</html>
